package com.qcby.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.entity.TProductSpecification;
import com.qcby.form.TProductSpecificationFrom;
import com.qcby.mapper.TProductSpecificationMapper;
import com.qcby.service.TProductSpecificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.vo.PageVO;
import com.qcby.vo.TProductSpecificationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品规格 服务实现类
 * </p>
 *
 * @author admin
 * @since 2025-02-04
 */
@Service
public class TProductSpecificationServiceImpl extends ServiceImpl<TProductSpecificationMapper, TProductSpecification> implements TProductSpecificationService {
    @Autowired
    private TProductSpecificationMapper specificationMapper;

    @Override
    public PageVO specificationList(TProductSpecificationFrom specificationFrom) {
        Page<TProductSpecification> page = new Page<>(specificationFrom.getPage(), specificationFrom.getLimit());
        QueryWrapper<TProductSpecification> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(specificationFrom.getProductName()), "product_name", specificationFrom.getProductName())
                .like(StringUtils.isNotBlank(specificationFrom.getSpecification()), "specification", specificationFrom.getSpecification());
        Page<TProductSpecification> resultPage = this.specificationMapper.selectPage(page, queryWrapper);
        PageVO pageVO = new PageVO();
        pageVO.setTotalCount(resultPage.getTotal());
        pageVO.setPageSize(resultPage.getSize());
        pageVO.setCurrPage(resultPage.getCurrent());
        pageVO.setTotalPage(resultPage.getPages());
        List<TProductSpecificationVO> list = new ArrayList<>();
        for (TProductSpecification record : resultPage.getRecords()) {
            TProductSpecificationVO specificationVO = new TProductSpecificationVO();
            BeanUtils.copyProperties(record, specificationVO);
            list.add(specificationVO);
        }
        pageVO.setList(list);
        return pageVO;
    }

    @Override
    public List<TProductSpecificationVO> getSpecificationList(Integer productId) {
        QueryWrapper<TProductSpecification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId)
                .eq("is_deleted", 0)
                .orderByDesc("version");
                
        return specificationMapper.selectList(queryWrapper)
                .stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void addSpecification(TProductSpecificationFrom form) {
        TProductSpecification specification = new TProductSpecification();
        BeanUtils.copyProperties(form, specification);
        
        specification.setVersion(LocalDateTime.now());
        specification.setIsDeleted(0);
        
        specificationMapper.insert(specification);
    }

    @Override
    @Transactional
    public void updateSpecification(Integer id, TProductSpecificationFrom form) {
        TProductSpecification specification = checkSpecificationExists(id);
        
        BeanUtils.copyProperties(form, specification);
        specification.setVersion(LocalDateTime.now());
        
        specificationMapper.updateById(specification);
    }

    @Override
    @Transactional
    public void deleteSpecification(Integer id) {
        TProductSpecification specification = checkSpecificationExists(id);
        
        specification.setIsDeleted(1);
        specification.setVersion(LocalDateTime.now());
        
        specificationMapper.updateById(specification);
    }

    @Override
    @Transactional
    public void batchDeleteByProductId(Integer productId) {
        QueryWrapper<TProductSpecification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId)
                .eq("is_deleted", 0);
                
        List<TProductSpecification> specifications = specificationMapper.selectList(queryWrapper);
        
        for (TProductSpecification spec : specifications) {
            spec.setIsDeleted(1);
            spec.setVersion(LocalDateTime.now());
            specificationMapper.updateById(spec);
        }
    }

    @Override
    @Transactional
    public void batchFreezeByProductId(Integer productId) {
        QueryWrapper<TProductSpecification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId)
                .eq("is_deleted", 0);
                
        List<TProductSpecification> specifications = specificationMapper.selectList(queryWrapper);
        
        for (TProductSpecification spec : specifications) {
            spec.setIsDeleted(2); // 2表示冻结状态
            spec.setVersion(LocalDateTime.now());
            specificationMapper.updateById(spec);
        }
    }

    private TProductSpecification checkSpecificationExists(Integer id) {
        TProductSpecification specification = specificationMapper.selectById(id);
        if (specification == null || specification.getIsDeleted() == 1) {
            throw new BusinessException("商品规格不存在");
        }
        return specification;
    }

    private TProductSpecificationVO convertToVO(TProductSpecification specification) {
        TProductSpecificationVO vo = new TProductSpecificationVO();
        BeanUtils.copyProperties(specification, vo);
        return vo;
    }
}
